'use strict';
const base = require('basecommon');

const db = uniCloud.database();
let response = function(code, data, msg) {
	return {
		code: code,
		data: data,
		msg: msg || '成功'
	}
}

exports.main = async (event, context) => {
	console.log('调用时间', new Date().Format('Y-M-D HH:mm:ss'))
	console.log(event, '——————拿到的参数')
	// 登录注册
	if (event.api == 'userLogin' || event.api == 'userRegist') {
		if (event.phone) {
			if (event.api == 'userLogin') {
				return userLogin(event)
			} else if (event.api == 'userRegist') {
				return userRegist(event)
			}
		} else {
			return response(20000, undefined, '参数错误')
		}
	} else if (event.api == 'newAddRelationship') {
		if (!event.name || !event.phone || !event.authorId)
			return response(20000, event, '参数错误')
		return newAddRelationship(event)
	} else if (event.api == 'getRelationship') {
		return getRelationship(event)
	}
};
/**
 * 获取联系人列表
 * */
let getRelationship = async (param) => {
	if (!param.userId)
		return response(20000, undefined, '参数错误');
	let collection = await db.collection('bmRelationship');
	let result = await collection.where({
		authorId: param.userId
	}).get()
	return response(10000, result.data);
};
/**
 * 新增联系人
 * @param {String}  name 姓名
 * @param {String}  phone 手机号
 * @param {String}  avatarUrl 头像链接
 * @param {String}  authorId 创建者ID
 * */
let newAddRelationship = async (param) => {
	let collection = await db.collection('bmRelationship');
	let user = await collection.where({
		authorId: param.authorId,
		phone: param.phone
	}).get()
	console.log(user, '————————查询结果')
	if (user.data.length <= 0) {
		let newData = await collection.add(param);
		console.log(newData, '_____添加陈宫')
		let data = await collection.doc(newData.id).get()
		return response(10000, data.data[0])
	} else {
		return response(20000, param, '该手机号已添加')
	}
};
/**
 * 登录
 * @param phone  手机号
 * @param password   密码
 * */
let userLogin = async (param) => {
	let collection = await db.collection('bmUser');
	let user = await collection.where({
		phone: param.phone
	}).get()
	console.log(user, '___杂货是')
	if (user.data.length > 0) {
		var isPasswoedUser; // 校验密码
		user.data.forEach(item => {
			if (item.password == param.password) {
				isPasswoedUser = item
			}
		})
		if (isPasswoedUser) {
			return response(10000, isPasswoedUser)
		} else {
			return response(20000, undefined, '密码不正确')
		}
	} else {
		return response(30001, undefined, '账号未注册')
	}
};
/**
 * 注册/更新用户信息
 * @param phone  手机号
 * @param name   姓名
 * @param password   密码
 * @param avatar 头像
 * @param userId 用户唯一表示-> 更新时会用到
 * */
let userRegist = async (param) => {
	let collection = await db.collection('bmUser');
	let user = await collection.where({
		phone: param.phone
	}).get()
	console.log(user, '——————是是是');
	if (user.data.length <= 0) {
		user = await collection.add({
			name: param.name,
			password: param.password,
			phone: param.phone,
			avatar: '',
			authority: true,
			createTime: param.createTime,
			createTimeStr: param.createTimeStr
		})
		let data = await collection.doc(user.id).get()
		return response(10000, data.data[0])
	} else {
		// 先判断一下是否有 userId没有则代表该手机号已注册
		if (!param.userId)
			return response(20000, undefined, '该账号已注册,请输入新的账号')

		const dbCmd = db.command
		// let res = await db.collection('photo').doc('doc-id').update({
		//   count: dbCmd.set({
		//     fav: 1,
		//     follow: 1
		//   })
		// })
		var currentUser;
		// console.log(user,'___让我看看你')
		user.data.forEach(item => {
			if (item._id == param.userId) {
				currentUser = item
			}
		})
		// console.log(currentUser, '___让我看看你1111——————')
		if (currentUser) {
			var res;
			if (param.avatar) {
				res = await collection.doc(currentUser._id).update({
					avatar: dbCmd.set(param.avatar)
				});
			}
			return response(10000, res)
		} else {
			return response(20000, undefined, '用户不存在')
		}
	}
}
